import model.DoubleLinkNode;
import utils.DoubleLinkUtil;

/**
 * 双向链表应用
 */
public class DoubleLinkApplication {
    public static void main(String[] args) {
        final int size = 10;
        final int updateValue = 61;
        final int defaultValue = -1;
        final int site = 4;
        final int value = 50;
        // TODO: 2023/11/6 实例化对象
        final DoubleLinkUtil doubleLinkUtil = new DoubleLinkUtil();
        // TODO: 2023/11/7 实例化双向链表，并将单向的首元素地址保存下来
        System.out.println("实例化双向链表对象");
        DoubleLinkNode head = doubleLinkUtil.linkInitiate(size);
        if (head != null) {
            doubleLinkUtil.printLink(head);
        }
        // TODO: 2023/11/7 插入新元素到指定序号元素的后面
        System.out.println();
        System.out.println(String.format("在序号为%d的元素后面，新插入值为：%d的元素", site, value));
        final DoubleLinkNode insertLinkNode = doubleLinkUtil.insertDate(head, site, value);
        if (insertLinkNode != null) {
            head = insertLinkNode;
            doubleLinkUtil.printLink(head);
        } else {
            System.out.println(String.format("预计插入的序号%s不合法！", site));
        }
        // TODO: 2023/11/7 根据序号，删除该元素
        System.out.println();
        System.out.println(String.format("删除序号为%d的元素", site));
        final DoubleLinkNode deleteLinkNode = doubleLinkUtil.deleteData(head, site);
        if (deleteLinkNode != null) {
            head = deleteLinkNode;
            doubleLinkUtil.printLink(head);
        } else {
            System.out.println(String.format("预计删除的序号%s不合法！", site));
        }
        // TODO: 2023/11/7 根据序号，修改该元素的值
        System.out.println();
        System.out.println(String.format("将序号为%d的元素的值改为：%d", site, updateValue));
        final DoubleLinkNode updateLinkNode = doubleLinkUtil.updateData(head, site, updateValue);
        if (updateLinkNode != null) {
            head = updateLinkNode;
            doubleLinkUtil.printLink(head);
        } else {
            System.out.println(String.format("预计修改的序号%s不合法！", site));
        }
        // TODO: 2023/11/7 销毁单向链表
        System.out.println();
        System.out.println(String.format("销毁所有元素，即将其值改为：%d", defaultValue));
        if (doubleLinkUtil.destroyLink(head)) {
            doubleLinkUtil.printLink(head);
        } else {
            System.out.println("单向链表为空，销毁失败！");
        }
    }
}
